﻿using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Reflection;

namespace WPFEdit.Views
{
    /// <summary>
    /// Interaction logic for TreeWindow.xaml
    /// </summary>
    public partial class TreeWindow : UserControl
    {
		private static object[] s_emptyObject = new object[] { };
        public static readonly string ContextMenuItemsBindingName = "ContextMenuItems";
		private bool m_doubleClickReady = false;

        public TreeWindow()
        {
            InitializeComponent();
        }


        protected void OnItemDoubleClick(object sender, MouseButtonEventArgs e)
        {
			m_doubleClickReady = !m_doubleClickReady;//have to counter for this getting called twice
            if (ItemDoubleClick != null && this.WindowTreeView.SelectedItem != null && m_doubleClickReady)
            {
                ItemDoubleClick(this.WindowTreeView.SelectedItem, e);
            }
        }

        protected void OnItemRightClick(object sender, MouseButtonEventArgs e)
        {
            ContextMenu menu = this.Resources["ItemContextMenu"] as ContextMenu;

            TreeViewItem item = sender as TreeViewItem;

            if (item != null)
            {
                item.IsSelected = true;
                object dataContext = item.DataContext;

                if (dataContext != null)
                {
                    //On-the-fly data binding of propery 'ContextMenuItems' to the context menu
                    PropertyInfo prop = dataContext.GetType().GetProperty(TreeWindow.ContextMenuItemsBindingName);

                    if (prop != null)
                    {
						IEnumerable iter = prop.GetValue(dataContext, s_emptyObject) as IEnumerable;

                        menu.ItemsSource = iter;
                    }
                }
            }

            e.Handled = true;
        }

        public bool ShowContextMenus
        {
            get
            {
                ContextMenu menu = this.Resources["ItemContextMenu"] as ContextMenu;
                return menu.Visibility == System.Windows.Visibility.Visible;
            }
            set
            {
                ContextMenu menu = this.Resources["ItemContextMenu"] as ContextMenu;
                menu.Visibility = (value) ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;

            }
        }

        public object SelectedItem
        {
            get { return WindowTreeView.SelectedItem; }
        }

        public TreeView TreeViewControl
        {
            get { return WindowTreeView; }
        }

        public event MouseButtonEventHandler ItemDoubleClick;
    }
}
